package it.low.game.client.gui.panel;

import it.low.game.client.GameClient;
import it.low.game.client.GameSession;
import it.low.game.client.gui.graphics.GraphicElement;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;

import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;

public class GameBoardPanel extends JPanel {
	
	private Border gameFieldBorder = new EtchedBorder(EtchedBorder.RAISED, Color.DARK_GRAY, Color.BLACK);
	
	private static GameView gameView = null;
	
	public GameBoardPanel(Point panelLocation){
		this.setLayout(new BorderLayout());
		this.setBackground(Color.GRAY);
		this.setBorder(gameFieldBorder);
				
		gameView = new GameView(panelLocation,GameClient.VIEW_WIDTH,GameClient.VIEW_HEIGHT);
	}
	
	protected void paintComponent(Graphics g){
		g.setClip(gameView);
		g.translate(gameView.x, gameView.y);
		GameSession gameSession = GameClient.getGameSession();
		if(gameSession!=null){
			if(gameSession.getMapBackground()!=null){
				gameSession.getMapBackground().draw(g);
			}
			if(gameSession.isRunning()){
				if(gameSession.getPlayer()!=null){
					gameSession.getPlayer().draw(g);
				}
				if(gameSession.getPlaceholder()!=null){
					gameSession.getPlaceholder().draw(g);
				}			
				if(gameSession.getGraphicElements()!=null){
					for(GraphicElement mapElement : gameSession.getGraphicElements()){
						mapElement.draw(g);
					}
				}
			}
		}
		g.clipRect(gameView.x, gameView.y, gameView.width, gameView.height);
		g.dispose();
	}
	
	public static boolean isGameViewArea(Point point){
		return gameView.contains(point);
	}
	
	public static Point getGamePanelRelativePoint(Point screenPoint){
		int gamePanelPointX = screenPoint.x - gameView.x;
		int gamePanelPointY = screenPoint.y - gameView.y;
		return new Point(gamePanelPointX,gamePanelPointY);
	}
	
	public static Point getScreenRelativePoint(Point gamePanelPoint){
		int screenPointX = gamePanelPoint.x + gameView.x;
		int screenPointY = gamePanelPoint.y + gameView.y;
		return new Point(screenPointX,screenPointY);
	}
	
	public class GameView extends Rectangle{	
		public GameView(Point offset, int width, int height){
			super(offset,new Dimension(width,height));			
		}		
	}
	
}